package com.xuanting.xuanting

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initMCL()
        initTablayout()
    }

    private fun initMCL() {
        mcl.setTitleListener(object :MyCoordinatorLayout.TitleChangeListener{
            override fun newTitle() {
                tv_title.text="标题栏，改变之后"
            }
            override fun oldTitle() {
                tv_title.text="标题栏，改变之前"
            }
        })
    }


    private fun initTablayout() {
        val fragments = listOf(
                TestFragment.newInstance(),
                TestFragment.newInstance(),
                TestFragment.newInstance(),
                TestFragment.newInstance())
        val names = listOf("one", "two", "three", "four")

        val adapter = TabAdapter(supportFragmentManager, fragments, names)
        vp_pager.adapter = adapter
        vp_pager.offscreenPageLimit = fragments.size
        tab.setupWithViewPager(vp_pager)
        //设置可以滑动
        tab.tabMode = TabLayout.MODE_SCROLLABLE
    }

    class TabAdapter(fm: FragmentManager, private val fragments: List<Fragment>, private val tabTitle: List<String>) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }

        override fun getCount(): Int {
            return fragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return tabTitle[position]

        }
    }
}
